package com.wefans.wefansapp.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.data.ResGetVoteOne;
import com.wefans.wefansapp.common.tool.data.ResGetVoteOne.ResGetVoteOneCateData;
import com.wefans.wefansapp.common.tool.data.ResGetVoteOne.ResGetVoteOneData;
import com.wefans.wefansapp.common.tool.data.ResSetVote;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.finals.FEATURES;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetImage;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;

/**
 * 메인화면(투표하기)
 * 
 * @author kimhyeongbae
 */
public class MainDetailActivity extends BaseActivity implements DrawerListener, IOnNetworkListener
{
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // final/static property
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static String INTENT_EXTRA_TAG_VOTE_IDX          = "INTENT_EXTRA_TAG_VOTE_IDX";
    public static String INTENT_EXTRA_TAG_VOTE_CATEGORY_IDX = "INTENT_EXTRA_TAG_VOTE_CATEGORY_IDX";
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // normal property
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private DrawerLayout     m_DrawerLayout    = null;
    private FrameLayout      m_DrawerView      = null;
    
    private MFNetworkManager m_oNetworkManager = null;
    private MFDataManager    m_oDataManager    = null;
    
    private String           m_strIntentExtraVoteIdx         = null;
    private String           m_strIntentExtraVoteCategoryIdx = null;
    
    private ResGetVoteOne    m_oResGetVoteOne  = null;
    private boolean          m_bVote           = false;
    
    private ListView         m_lvContents      = null;
    
    private int              m_nNonGraphIndex  = 0;
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // life-cycle method
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_detail);
        
        MFDebug.d(">> onCreate()");
        
        initialize();
        installEvent();
    }

    @Override
    public void finish()
    {
        MFDebug.d(">> finish()");
        
        super.finish();
    }
    
    @Override
    protected void onPause()
    {
        MFDebug.d(">> onPause()");
        
        notifyDoClaseDrawerMenu();
        
        super.onPause();
    }

    @Override
    protected void notifyDoClaseDrawerMenu()
    {
        MFDebug.d(">> notifyDoClaseDrawerMenu()");
        
        super.notifyDoClaseDrawerMenu();
        
        if( m_DrawerLayout == null ) {
            MFDebug.e(new Exception("There are NO Drawer-Layout"));
            return;
        }
        
        if( m_DrawerView == null ) {
            MFDebug.e(new Exception("There are NO Drawer-View"));
            return;
        }
        
        m_DrawerLayout.closeDrawer(m_DrawerView);
        m_sdQuickMenu.close();
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // normal method
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private void initialize()
    {
        MFDebug.d(">> initialize()");
        
        m_oNetworkManager = MFNetworkManager.getInstance(this);
        m_oDataManager    = MFDataManager.getInstance(this);
        
        m_strIntentExtraVoteIdx         = getIntent().getStringExtra(INTENT_EXTRA_TAG_VOTE_IDX);
        m_strIntentExtraVoteCategoryIdx = getIntent().getStringExtra(INTENT_EXTRA_TAG_VOTE_CATEGORY_IDX);
        MFDebug.d("++ m_strIntentExtraVoteIdx : " + m_strIntentExtraVoteIdx);
        MFDebug.d("++ m_strIntentExtraVoteCategoryIdx : " + m_strIntentExtraVoteCategoryIdx);
        
        //[Step 1] Contents layout adding
        FrameLayout containerContents = (FrameLayout) findViewById(R.id.FL_MAIN_DETAIL_CONTAINER);
        addContentsLayout(containerContents, R.layout.layout_main_detail_contents);
        addQuickMenuLayout(containerContents);
        
        //[Step 2] Drawer menu adding
        m_DrawerView = (FrameLayout) findViewById(R.id.FL_MAIN_DETAIL_DRAWER_LAYOUT);
        addDrawerLayout(m_DrawerView);
        
        //[Step 3] set drawer layout (root)
        m_DrawerLayout = (DrawerLayout) findViewById(R.id.DL_MAIN_DETAIL_NAVIGATION_LAYOUT);
        
        //[Step 4] request server data
        {
            requestGetVoteOne();
        }
        
        m_lvContents = (ListView) findViewById(R.id.LV_MAIN_DETAIL_BODY_GRAPH_ON_LAYOUT);
        m_lvContents.setAdapter(null);
    }
    
    private void installEvent()	
    {
        MFDebug.d(">> installEvent()");
        
        ((ImageView) findViewById(R.id.IV_MAIN_DETAIL_BODY_GRAPH_ON_OFF)).setOnClickListener(this);
        ((ImageButton) findViewById(R.id.IB_MAIN_DETAIL_HEADER_LEFT_BUTTON)).setOnClickListener(this);
        ((ImageButton) findViewById(R.id.IB_MAIN_DETAIL_BODY_SORT_POPULAR)).setOnClickListener(this);
        ((ImageButton) findViewById(R.id.IB_MAIN_DETAIL_BODY_SORT_NAME)).setOnClickListener(this);
        ((LinearLayout) findViewById(R.id.LL_MAIN_DETAIL_BODY_VOTE_REPLY)).setOnClickListener(this);
        
        ((ImageButton) findViewById(R.id.IB_MAIN_DETAIL_BODY_GRAPH_OFF_BACK_BTN)).setOnClickListener(this);
        ((ImageButton) findViewById(R.id.IB_MAIN_DETAIL_BODY_GRAPH_OFF_NEXT_BTN)).setOnClickListener(this);
        ((ImageButton) findViewById(R.id.IB_MAIN_DETAIL_BODY_GRAPH_OFF_VOTE_BTN)).setOnClickListener(this);
    }
    
    /**
     * 투표상세 데이터를 요청한다.
     * 
     */
    private void requestGetVoteOne()
    {
        MFDebug.d(">> requestGetVoteOne()");
        
        m_oNetworkManager.requestGetVoteOne(this, m_strIntentExtraVoteIdx, m_strIntentExtraVoteCategoryIdx);
    }
    
    /**
     * 투표를 진행한다.
     * 
     * @param questionIdx
     */
    private void requestSetVote(ResGetVoteOneData item)
    {
        MFDebug.d(">> requestSetVote()");
        MFDebug.d("++ item : " + item);
        
        if( item == null ) {
            MFDebug.e(new Exception("There are NO selected item"));
            return;
        }
        
        if( m_oResGetVoteOne == null ) {
            MFDebug.e(new Exception("There are NO ResGetVoteOne"));
            return;
        }
        
        if( m_oResGetVoteOne.getMain_data() == null ) {
            MFDebug.e(new Exception("There are NO ResGetVoteOne.main_data"));
            return;
        }
        
        m_oNetworkManager.requestSetVote(this
                , m_oDataManager.getMebmerIdx()
                , m_oResGetVoteOne.getMain_data().getIdx()
                , m_oResGetVoteOne.getMain_data().getCate_idx()
                , item.getIdx(), "1"
                , FEATURES.APP_VERSION
                , "A");
    }
    
    /**
     * 그래프모드 여부 반환
     * 
     * @return
     */
    private boolean isGraphMode()
    {
        MFDebug.d(">> isGraphMode()");
        
        ListView lvGraphView = (ListView) findViewById(R.id.LV_MAIN_DETAIL_BODY_GRAPH_ON_LAYOUT);
        
        return ( lvGraphView.getVisibility() == View.VISIBLE ) ? true : false;
    }
    
    /**
     * 화면 모드를 그래프 보기모드로 전환 또는 비전환 한다.
     * 
     */
    private void changeGraphMode()
    {
        MFDebug.d(">> changeGraphMode()");
        
        ListView lvGraphView = (ListView) findViewById(R.id.LV_MAIN_DETAIL_BODY_GRAPH_ON_LAYOUT);
        LinearLayout llNonGraphView = (LinearLayout) findViewById(R.id.LL_MAIN_DETAIL_BODY_GRAPH_OFF_LAYOUT);
        ImageView ivBtnChangeMode = (ImageView) findViewById(R.id.IV_MAIN_DETAIL_BODY_GRAPH_ON_OFF);
        
        lvGraphView.setVisibility((isGraphMode()) ? View.GONE : View.VISIBLE);
        llNonGraphView.setVisibility((isGraphMode()) ? View.GONE : View.VISIBLE);
        
        if( isGraphMode() ) {
            ivBtnChangeMode.setBackgroundResource(R.drawable.graph_btn_off);
        }
        else {
            ivBtnChangeMode.setBackgroundResource(R.drawable.graph_btn_on);
        }
    }
    
    /**
     * 주어진 투표상세 정보내의 데이터를 List 로 반환 한다.
     * 
     * @param voteOne
     * @return
     */
    private List<ResGetVoteOneData> toArray(ResGetVoteOne voteOne)
    {
        List<ResGetVoteOneData> listResGetVoteOneData = new ArrayList<ResGetVoteOne.ResGetVoteOneData>();
        
        Enumeration<String> enumeration = voteOne.getData().get(0).keys();
        int i=0;
        while(enumeration.hasMoreElements()) {
            String key = enumeration.nextElement().toString();
            ResGetVoteOneData item = voteOne.getData().get(0).get(key);
            listResGetVoteOneData.add(item);
        }
        
        return listResGetVoteOneData;
    }
    
    /**
     * 화면에 데이터를 채운다.
     * 
     * @param voteOne
     */
    private void fillContents(ResGetVoteOne voteOne)
    {
        MFDebug.d(">> fillContents()");
        MFDebug.d("++ voteOne : " + voteOne);
        
        //[Step 1] Title, Description, Reply count
        TextView tvVoteOneTitle = (TextView) findViewById(R.id.TV_MAIN_DETAIL_BODY_VOTE_TITLE);
        tvVoteOneTitle.setText(voteOne.getMain_data().getTitle());
        
        TextView tvVoteOneDescription = (TextView) findViewById(R.id.TV_MAIN_DETAIL_BODY_VOTE_DESCRIPTION);
        tvVoteOneDescription.setText(voteOne.getMain_data().getSub_title());//TODO API 규격상에 해당 필드가 존재하지 않음
        
        TextView tvVoteOneReplyCount = (TextView) findViewById(R.id.TV_MAIN_DETAIL_BODY_VOTE_REPLY);
        tvVoteOneReplyCount.setText(voteOne.getMain_data().getComment_cnt());
        
        //[Step 2] Category 셋팅
        TextView tvToday = (TextView) findViewById(R.id.TV_MAIN_DETAIL_BODY_VOTE_STATUS_TITLE);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy.MM.dd ");
        tvToday.setText(sf.format(new Date(System.currentTimeMillis())) + getResources().getString(R.string.main_detail_current_voting_status));
        
        /** Tab 레이아웃 셋팅 */
        fillContentsTabLayou(voteOne);
        
        /** TODO : 텍스트 비교 하면 안된다. 서버에서 오는 텍스트를 가지고 표시해야 할 듯 */
        ImageView ivShareGap = (ImageView) findViewById(R.id.IV_MAIN_DETAIL_BODY_STATUS_TEXT_IMG);
        /*if( voteOne.getMain_data().getShare_gap_text().equals("now") ) {
         * 
        }
        else*/ if( voteOne.getMain_data().getShare_gap_text().equals("경합중") ) {
            ivShareGap.setBackgroundResource(R.drawable.vote_ing);
        }
        else {
            ivShareGap.setBackgroundResource(R.drawable.vote_ing02);
        }
        
        fillGraphLayout(voteOne);
        fillNonGraphLayout(voteOne);
    }
    
    /**
     * Tab 레이아웃을 채운다.
     * 
     * @param voteOne
     */
    private void fillContentsTabLayou(ResGetVoteOne voteOne)
    {
        MFDebug.d(">> fillContents()");
        MFDebug.d("++ voteOne : " + voteOne);
        
        LinearLayout rootLayout = (LinearLayout) findViewById(R.id.LL_MAIN_DETAIL_BODY_TAB_ROOT);
        
        List<ResGetVoteOneCateData> listCateData = voteOne.getCate_data();
        if( listCateData != null && listCateData.size() > 0 ) {
            LinearLayout[] tabs = 
                {
                    (LinearLayout) findViewById(R.id.LL_MAIN_DETAIL_BODY_TAB_1ST),
                    (LinearLayout) findViewById(R.id.LL_MAIN_DETAIL_BODY_TAB_2ST),
                    (LinearLayout) findViewById(R.id.LL_MAIN_DETAIL_BODY_TAB_3ST)
                };
            TextView[] tabTitles =
                {
                    (TextView) findViewById(R.id.TV_MAIN_DETAIL_BODY_TAB_1ST),
                    (TextView) findViewById(R.id.TV_MAIN_DETAIL_BODY_TAB_2ST),
                    (TextView) findViewById(R.id.TV_MAIN_DETAIL_BODY_TAB_3ST)
                };
            tabs[0].setVisibility(View.GONE);
            tabs[1].setVisibility(View.GONE);
            tabs[2].setVisibility(View.GONE);
            
            int size = listCateData.size();
            if( size < 2 ) {
                rootLayout.setVisibility(View.GONE);
            }
            else {
                rootLayout.setVisibility(View.VISIBLE);
            }
            
            for( int i=0; i<size; i++ ) {
                ResGetVoteOneCateData cateData = listCateData.get(i);
                tabTitles[i].setText(cateData.getTitle());
                tabs[i].setVisibility(View.VISIBLE);
            }
        }
    }
    
    /**
     * 그래프모드 화면을 채운다.
     * 
     * @param voteOne
     */
    private void fillGraphLayout(ResGetVoteOne voteOne)
    {
        MFDebug.d(">> fillGraphLayout()");
        
        //ListView
        List<ResGetVoteOneData> listResGetVoteOneData = toArray(voteOne);
        for( int i=0; i<listResGetVoteOneData.size(); i++ ) {
            ResGetVoteOneData item = listResGetVoteOneData.get(i);
            m_oNetworkManager.requestImageData(i, item.getImg_url(), this);
        }
        CAdapter adapter = new CAdapter(this, -1, -1, listResGetVoteOneData);
        m_lvContents.setAdapter(adapter);
    }
    
    /**
     * 일반 모드 화면을 채운다.
     * 
     * @param voteOne
     */
    private void fillNonGraphLayout(ResGetVoteOne voteOne)
    {
        MFDebug.d(">> fillNonGraphLayout()");
        
        CAdapter adapter = (CAdapter) m_lvContents.getAdapter();
        if( adapter == null ) {
            MFDebug.e(new Exception("Threre are NO adapter"));
            return;
        }
        
        List<ResGetVoteOneData> listResGetVoteOneData = adapter.getListItem();
        if( listResGetVoteOneData == null || listResGetVoteOneData.size() < 1 ) {
            MFDebug.e(new Exception("Threre are NO items"));
            return;
        }
        
        TextView  tvRanking      = (TextView)  findViewById(R.id.TV_MAIN_DETAIL_BODY_GRAPH_OFF_RANKING);
        ImageView ivRankingBg    = (ImageView) findViewById(R.id.IV_MAIN_DETAIL_BODY_GRAPH_OFF_BG);
        ImageView ivProfileFrame = (ImageView) findViewById(R.id.IV_MAIN_DETAIL_BODY_GRAPH_OFF_STAR_PROFILE_IMG_FRAME);
        ImageView ivProfile      = (ImageView) findViewById(R.id.IV_MAIN_DETAIL_BODY_GRAPH_OFF_STAR_PROFILE_IMG);
        TextView  tvRatio        = (TextView)  findViewById(R.id.TV_MAIN_DETAIL_BODY_GRAPH_OFF_VOTE_PERSENTEGE);
        TextView  tvStarName     = (TextView)  findViewById(R.id.TV_MAIN_DETAIL_BODY_GRAPH_OFF_STAR_NAME);
        
        final ResGetVoteOneData item = listResGetVoteOneData.get(m_nNonGraphIndex);
        if( item == null ) {
            MFDebug.e(new Exception("There are NO item"));
            return;
        }
        
        tvRanking.setText(item.getRanking() + getResources().getString(R.string.main_detail_ranking));
        
        switch( m_nNonGraphIndex ) {
        case 0:
            ivRankingBg.setImageResource(R.drawable.ranking_1st);
            ivProfileFrame.setImageResource(R.drawable.a_kimhyeongbae_main_detail_no_graph_rankin1_singer_img);
            break;
        case 1:
            ivRankingBg.setImageResource(R.drawable.ranking_2nd);
            ivProfileFrame.setImageResource(R.drawable.a_kimhyeongbae_main_detail_no_graph_rankin2_singer_img);
            break;
        case 2:
            ivRankingBg.setImageResource(R.drawable.ranking_4th);
            ivProfileFrame.setImageResource(R.drawable.a_kimhyeongbae_main_detail_no_graph_rankin4_singer_img);
            break;
        case 3:
            ivRankingBg.setImageResource(R.drawable.ranking_8th);
            ivProfileFrame.setImageResource(R.drawable.a_kimhyeongbae_main_detail_no_graph_rankin8_singer_img);
        default:
            break;
        }
        
        byte[] buffer = item.getImageBuffer();
        if( buffer != null ) {
            Bitmap bm = BitmapFactory.decodeByteArray(item.getImageBuffer(), 0, item.getImageBuffer().length);
            ivProfile.setImageBitmap(bm);
        }
        
        tvRatio.setText(item.getRatio());
        
        tvStarName.setText(item.getTitle());
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // network interface
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public void onNetMessage(int resultCode, NetMessage message)
    {
        MFDebug.d(">> onNetMessage()");
        MFDebug.d("++ resultCode : " + resultCode);
        MFDebug.d("++ message : " + message);
        
        try {
            //서버 여동 데이터
            if( message instanceof NetJsonData ) {
                long magicNum = ((NetJsonData) message).getMagicNum();
                String json = ((NetJsonData) message).getResponseJson();
                
                if( json == null ) {
                    throw new Exception("There are NO json data");
                }
                
                Gson gson = new Gson();
                
                if( magicNum == MFNetworkManager.REQUEST_GET_VOTE_ONE ) {
                    ResGetVoteOne voteOne = (ResGetVoteOne) gson.fromJson(json, ResGetVoteOne.class);
                    
                    if( voteOne == null ) {
                        throw new Exception("Gson parsing error");
                    }
                    m_oResGetVoteOne = voteOne;
                    m_bVote = (voteOne.getMain_data().getIs_vote().equals("0")) ? false : true;
                    
                    fillContents(voteOne);
                }
                else if( magicNum == MFNetworkManager.REQUEST_SET_VOTE ) {
                    ResSetVote vote = (ResSetVote) gson.fromJson(json, ResSetVote.class);
                    
                    if( vote == null ) {
                        throw new Exception("Gson parsing error");
                    }
                    
                    if( vote.isSuccsess() == false ) {
                        Toast.makeText(getApplicationContext(), vote.getResultmsg(), Toast.LENGTH_SHORT).show();
                    }
                }
            }
            //이미지 데이터
            else if( message instanceof NetImage ) {
                long   magicNum = ((NetImage)message).getMagicNum();
                String url      = ((NetImage)message).getURL();
                byte[] buffer   = ((NetImage)message).getResponseBinary();
                
                if( buffer == null ) {
                    throw new Exception("There are NO image data");
                }
                
                CAdapter adapter = (CAdapter) m_lvContents.getAdapter();
                adapter.getItem((int)magicNum).setImageBuffer(buffer);
                adapter.notifyDataSetChanged();
                
                if( m_nNonGraphIndex == (int)magicNum ) {
                    fillNonGraphLayout(m_oResGetVoteOne);
                }
            }
            else {
                //ignore...
            }
        } catch(Exception e) {
            MFDebug.e(e);
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // interface method
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public void onClick(View v)
    {
        MFDebug.d(">> onClick()");
        
        switch( v.getId() ) {
            case R.id.IB_MAIN_DETAIL_HEADER_LEFT_BUTTON:
                finish();
                break;
            case R.id.IV_MAIN_DETAIL_BODY_GRAPH_ON_OFF:
                changeGraphMode();
                break;
            case R.id.IB_MAIN_DETAIL_BODY_SORT_POPULAR: {
                    CAdapter adapter = (CAdapter) m_lvContents.getAdapter();
                    List<ResGetVoteOneData> listItem = adapter.getListItem();
                    Collections.sort(listItem, new Comparator<ResGetVoteOneData>()
                    {
                        @Override
                        public int compare(ResGetVoteOneData lhs, ResGetVoteOneData rhs)
                        {
                            int left = Integer.parseInt(lhs.getRanking());
                            int right = Integer.parseInt(rhs.getRanking());
                            
                            return (left > right) ? 1 : -1;
                        }
                    });
                    adapter.notifyDataSetChanged();
                    fillNonGraphLayout(m_oResGetVoteOne);
                }
                break;
            case R.id.IB_MAIN_DETAIL_BODY_SORT_NAME: {
                    CAdapter adapter = (CAdapter) m_lvContents.getAdapter();
                    List<ResGetVoteOneData> listItem = adapter.getListItem();
                    Collections.sort(listItem, new Comparator<ResGetVoteOneData>()
                    {
                        @Override
                        public int compare(ResGetVoteOneData lhs, ResGetVoteOneData rhs)
                        {
                            return lhs.getTitle().compareTo(rhs.getTitle());
                        }
                    });
                    adapter.notifyDataSetChanged();
                    fillNonGraphLayout(m_oResGetVoteOne);
                }
                break;
            case R.id.LL_MAIN_DETAIL_BODY_VOTE_REPLY :
                Intent intent = new Intent(this, ReplyActivity.class);
                intent.putExtra(ReplyActivity.INTENT_EXTRA_TAG_REPLY_IDX, m_strIntentExtraVoteIdx);
                startActivity(intent);
                break;
                
            case R.id.IB_MAIN_DETAIL_BODY_GRAPH_OFF_BACK_BTN :
                {
                    CAdapter adapter = (CAdapter) m_lvContents.getAdapter();
                    List<ResGetVoteOneData> listItem = adapter.getListItem();
                    if( listItem == null || listItem.size() < 1 ) {
                        break;
                    }
                    
                    m_nNonGraphIndex--;
                    if( m_nNonGraphIndex < 0 ) {
                        m_nNonGraphIndex = listItem.size()-1;
                    } 
                    fillNonGraphLayout(m_oResGetVoteOne);
                }
                break;
            case R.id.IB_MAIN_DETAIL_BODY_GRAPH_OFF_NEXT_BTN :
                {
                    CAdapter adapter = (CAdapter) m_lvContents.getAdapter();
                    List<ResGetVoteOneData> listItem = adapter.getListItem();
                    if( listItem == null || listItem.size() < 1 ) {
                        break;
                    }
                    
                    m_nNonGraphIndex++;
                    if( m_nNonGraphIndex > listItem.size()-1 ) {
                        m_nNonGraphIndex = 0;
                    }
                    fillNonGraphLayout(m_oResGetVoteOne);
                }
                break;
            case R.id.IB_MAIN_DETAIL_BODY_GRAPH_OFF_VOTE_BTN :
                {
                    CAdapter adapter = (CAdapter) m_lvContents.getAdapter();
                    List<ResGetVoteOneData> listItem = adapter.getListItem();
                    if( listItem == null || listItem.size() < 1 ) {
                        break;
                    }
                    
                    requestSetVote(listItem.get(m_nNonGraphIndex));
                }
                break;
            default :
                break;
        }
        
        super.onClick(v);
    }

    @Override
    public void onDrawerClosed(View drawerView)
    {
        MFDebug.d(">> onDrawerClosed()");
        MFDebug.d("++ drawerView : " + drawerView);
    }

    @Override
    public void onDrawerOpened(View drawerView)
    {
        MFDebug.d(">> onDrawerOpened()");
        MFDebug.d("++ drawerView : " + drawerView);
    }

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset)
    {
        MFDebug.d(">> onDrawerSlide()");
        MFDebug.d("++ drawerView : " + drawerView);
        MFDebug.d("++ slideOffset : " + slideOffset);
    }

    @Override
    public void onDrawerStateChanged(int newState)
    {
        MFDebug.d(">> onDrawerStateChanged()");
        MFDebug.d("++ newState : " + newState);
        
        String state = null;
        switch (newState) {
        case DrawerLayout.STATE_IDLE:
            state = "STATE_IDLE";
            break;
        case DrawerLayout.STATE_DRAGGING:
            state = "STATE_DRAGGING";
            break;
        case DrawerLayout.STATE_SETTLING:
            state = "STATE_SETTLING";
            break;
        default:
            state = "unknown!";
        }
        MFDebug.d("++ state : " + state);
    }
    
    /******************************************************************************************************************
     * 메인상세(투표하기) 리스트 어뎁터클래스
     * 
     * @author kimhyeongbae
     *
     ******************************************************************************************************************/
    class CAdapter extends ArrayAdapter<ResGetVoteOneData>
    {
        private final float MAX_GRAPH_LENGTH = getResources().getDimension(R.dimen.x_530);
        
        private LayoutInflater          m_oInflater           = null;
        private List<ResGetVoteOneData> m_listItem            = new ArrayList<ResGetVoteOne.ResGetVoteOneData>();

        public CAdapter(Context context, int resource, int textViewResourceId, List<ResGetVoteOneData> objects)
        {
            super(context, resource, textViewResourceId, objects);
            
            m_listItem = objects;
            
            m_oInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        
        public List<ResGetVoteOneData> getListItem()
        {
            return m_listItem;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if( convertView == null ) {
                convertView = m_oInflater.inflate(R.layout.list_item_main_detail_vote_list, null);
            }
            
            final ResGetVoteOneData item = m_listItem.get(position); 
            
            TextView    tvRanking        = (TextView) convertView.findViewById(R.id.IV_MAIN_DETAIL_LIST_ITEM_RANKING);
            ImageView   ivGraphLeft      = (ImageView) convertView.findViewById(R.id.IV_MAIN_DETAIL_LIST_ITEM_GRAPH_01);
            ImageView   ivGraph          = (ImageView) convertView.findViewById(R.id.IV_MAIN_DETAIL_LIST_ITEM_GRAPH_03);
            ImageView   ivGraphRight     = (ImageView) convertView.findViewById(R.id.IV_MAIN_DETAIL_LIST_ITEM_GRAPH_02);
            ImageView   ivStarProfileImg = (ImageView) convertView.findViewById(R.id.IV_MAIN_DETAIL_LIST_ITEM_STAR_PROFILE_IMG);
            TextView    tvStarName       = (TextView) convertView.findViewById(R.id.TV_MAIN_DETAIL_LIST_ITEM_STAR_NAME);
            TextView    tvRatio          = (TextView) convertView.findViewById(R.id.TV_MAIN_DETAIL_LIST_ITEM_VOTE_RATIO);
            ImageButton ibVote           = (ImageButton) convertView.findViewById(R.id.IB_MAIN_DETAIL_LIST_ITEM_VOTE_BTN);
            
            tvRanking.setText(item.getRanking()+getResources().getString(R.string.main_detail_ranking));
            
            int ranking = 1;
            try {
                ranking = Integer.parseInt(item.getRanking());
            } catch (Exception e) {
                MFDebug.e(e);
            }
            
            switch(ranking) {
                case 1:
                    ivGraphLeft.setBackgroundResource(R.drawable.vote_1st_01);
                    ivGraph.setBackgroundResource(R.drawable.vote_1st_03);
                    ivGraphRight.setBackgroundResource(R.drawable.vote_1st_02);
                    break;
                case 2:
                    ivGraphLeft.setBackgroundResource(R.drawable.vote_2nd_01);
                    ivGraph.setBackgroundResource(R.drawable.vote_2ndt_03);
                    ivGraphRight.setBackgroundResource(R.drawable.vote_2nd_02);
                    break;
                case 3:
                    ivGraphLeft.setBackgroundResource(R.drawable.vote_4th_01);
                    ivGraph.setBackgroundResource(R.drawable.vote_4th_03);
                    ivGraphRight.setBackgroundResource(R.drawable.vote_4th_02);
                    break;
                default:
                    ivGraphLeft.setBackgroundResource(R.drawable.vote_8th_01);
                    ivGraph.setBackgroundResource(R.drawable.vote_8th_03);
                    ivGraphRight.setBackgroundResource(R.drawable.vote_8th_02);
                    break;
            }
            
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ivGraph.getLayoutParams();
            params.width = (int)(MAX_GRAPH_LENGTH - (getResources().getDimension(R.dimen.x_20) * (ranking-1)));
            
            if(item.getImageBuffer() != null) {
                Bitmap bm = BitmapFactory.decodeByteArray(item.getImageBuffer(), 0, item.getImageBuffer().length);
                ivStarProfileImg.setImageBitmap(bm);
            }
            tvStarName.setText(item.getTitle());
            tvRatio.setText(item.getRatio() + "%");
            
            ibVote.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    requestSetVote(item);
                }
            });
            
            return convertView;
        }
    };
}
